Commands and Undo CPX
Volume Number: 11
Issue Number: 1
Column Tag: Visual Programming
Commands and Undo in Prograph CPX
Command objects to do, redo, and undo
By Kurt Schmucker, Apple Computer, Inc.
Note: Source code files accompanying article are located on MacTech CD-ROM or
source code disks.
When the Macintosh first came out in ‘84, there was one feature that users loved and
developers hated: undo. For users, knowing that any action could be undone reduced
dramatically the level of tension associated with using a new feature in a familiar
application, with using a new app, or even with using a computer in general.
Developers, unfortunately, had to implement this great functionality, and it wasn’t
easy. Storing sufficient state in an event-driven application so that any user action
could be undone was simply very difficult.
Command objects in MacApp 1.0 offered a way around this problem - a way that
was easy for developers both to comprehend and to implement. A command object is an
encapsulation of a user request for some action. It stores enough information to be able
to perform the action, to be able to undo the action, and to be able to redo the action,
and the methods of that command object perform exactly these functions. In response
to a user action, your code allocates an appropriate command object, initializes that
object, and then returns that object to the framework. The framework will
immediately send the “DoIt” message to this object to have the desired action
performed. The framework will hold onto that command object and will automatically
send the “UndoIt” and “RedoIt” messages to the object if the user chooses the Undo or
Redo menu items from the Edit menu. The developer is completely relieved of the
responsibility for the run-time handling of Undo.
To illustrate these ideas, I have implemented an enhanced version of the
MiniQuadWorld [1] application in CPX. The Prograph version of this app is on the
source code disks and the online sites, along with the full source code (both in standard
Prograph project and section files - for those of you who have CPX - and in PICTs for
those of you who don’t). Also, the code for this application follows the latest version of
the emerging Prograph coding style [2, 3].